/*
 *      time.c
 *
 *      Copyright 2007 Giulio Bai <giulio@hewle.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <time.h>

static int month[12] = {
	0,
	DAY * (31),
	DAY * (31 + 29),
	DAY * (31 + 29 + 31),
	DAY * (31 + 29 + 31 + 30),
	DAY * (31 + 29 + 31 + 30 + 31),
	DAY * (31 + 29 + 31 + 30 + 31 + 30),
	DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31),
	DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31),
	DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
	DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
	DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
};


unsigned long mktime (struct timek *time)
{
	unsigned long mtime;
	unsigned year;

	year = time->year - 70;
	
	mtime = YEAR * year + DAY * ((year + 1) / 4);
	mtime += month[time->mon];
	
	if (time->mon > 1 && ((year + 2) % 4)) {
		mtime -= DAY;
	}
	
	mtime += DAY * (time->day-1);
	mtime += HOUR * time->hour;
	mtime += MINUTE * time->min;
	mtime += time->sec;
	
	return mtime;
}


unsigned long timediff (unsigned long time1, unsigned long time2)
{
	return ((time1 >= time2) ? time1-time2 : time2-time1);
}
